package androidcamp.socialfitnesstracker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidcamp.socialfitnesstracker.data.Goal;
import androidcamp.socialfitnesstracker.data.TimeUnit;
import androidcamp.socialfitnesstracker.data.WorkoutType;

import java.util.ArrayList;

public class GoalsPrompt extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prompt);

		// set adapter for reps/time
        ArrayList<String> activities = new ArrayList<String>();
        for(WorkoutType type: WorkoutType.values()) {
            activities.add(type.getHumanName());
        }
		ArrayAdapter<String> repsOrTime = new ArrayAdapter<String>(this,
				R.layout.listexercise, activities);

		// first rep/time
		Spinner sp1 = (Spinner) findViewById(R.id.chooseWorkoutType);
		sp1.setAdapter(repsOrTime);
        
        // when submit button pressed
        ((Button) findViewById(R.id.submitbutton)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // get workout type
                int workoutTypeIndex = ((Spinner) findViewById(R.id.chooseWorkoutType)).getSelectedItemPosition();
                WorkoutType workoutType = WorkoutType.values()[workoutTypeIndex];
                // get time spent
                String text = ((EditText) findViewById(R.id.chooseWorkoutTimeSpent)).getText().toString();
                int timeToSpend = Integer.parseInt(text);
                // get interval
                String text1 = ((EditText) findViewById(R.id.chooseWorkoutInterval)).getText().toString();
                int interval = Integer.parseInt(text1);
                // construct goal
                Goal theGoal = new Goal(workoutType, timeToSpend*60*60, interval, TimeUnit.WEEK);
                // save goal
                MockDB.get().insertNewGoal(theGoal);
                // go back to goals view
                GoalsPrompt.this.finish();
            }
        });

	}
}
